package com.hzutitech.aqfxgk.android;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.hzutitech.aqfxgk.android.util.UploadUtil;

public class CameraActivity extends Activity {

	private ImageView preview;
	private Bitmap myBitmap;

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddkkmmss");
	private String imagePath;

	private static String requestURL = "http://www.lifecb.com/android/index.php?go=go";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.camera);
		preview = (ImageView) this.findViewById(R.id.imageView);
		Button btn = (Button) findViewById(R.id.btn_open);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				File imageFile = new File(imagePath);
				if (imageFile != null) {
					String request = UploadUtil.uploadFile(imageFile, requestURL);
					Toast.makeText(getApplicationContext(), request, 0).show();
				}
			}
		});

		Button btnUpload = (Button) findViewById(R.id.btn_upload);
		btnUpload.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
				startActivityForResult(getImageByCamera, 1);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		try {
			super.onActivityResult(requestCode, resultCode, data);
			Bundle extras = data.getExtras();
			myBitmap = (Bitmap) extras.get("data");
			preview.setImageBitmap(myBitmap);
			// 保存到SD卡上
			imagePath = "sdcard/aqfxgk/images/" + sdf.format(new Date()) + ".jpg";
			FileOutputStream out = new FileOutputStream(imagePath);
			myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}